package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.nhc.model.PointAZInfo;
import com.nhc.model.TwoPointsFileHandle;
import com.nhc.model.TwoPointsInfo;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.TopMenuWindow;

public class TwoPointsSetActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "TwoPointsSetActivity";

    public static final String INTENT_MERCATOR_MODE = "INTENT_MERCATOR_MODE";


    public static Intent createIntent(Context context, int mode) {
        return new Intent(context, TwoPointsSetActivity.class)
                .putExtra(INTENT_MERCATOR_MODE, mode);
    }


    private int _mode = 0;//0-Two Points 1-Point AZ
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    TwoPointsInfo mSelfInfo;
    PointAZInfo mPAZ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.twopointsset_activity);

        intent = getIntent();
        _mode = intent.getIntExtra(INTENT_MERCATOR_MODE, _mode);

        TwoPointsFileHandle.getInstance().mode = _mode;
        if (_mode == 0) {
            mPAZ = TwoPointsFileHandle.getInstance().mPAZ;
        } else {
            mSelfInfo = TwoPointsFileHandle.getInstance().mTwoPI;
        }

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ScrollView svDemoMain;

    private TextView tv_ctnipt;
    private TextView tv_lat_pt1;
    private TextView tv_lat_pt2;
    private TextView tv_lon_pt1;
    private TextView tv_lon_pt2;
    private TextView tv_utmc;
    private TextView tv_TY;
    private TextView tv_EW;
    private TextView tv_SN;

    private TextView tv_lon_c;
    private TextView tv_alpha_c;

    @Override
    public void initView() {//必须调用
        svDemoMain = (ScrollView) findViewById(R.id.svDemoMain);

        tv_ctnipt = (TextView) findViewById(R.id.tv_ctnipt);
        tv_lat_pt1 = (TextView) findViewById(R.id.tv_lat_pt1);
        tv_lat_pt2 = (TextView) findViewById(R.id.tv_lat_pt2);
        tv_lon_pt1 = (TextView) findViewById(R.id.tv_lon_pt1);
        tv_lon_pt2 = (TextView) findViewById(R.id.tv_lon_pt2);
        tv_utmc = (TextView) findViewById(R.id.tv_utmc);
        tv_TY = (TextView) findViewById(R.id.tv_TY);
        tv_EW = (TextView) findViewById(R.id.tv_EW);
        tv_SN = (TextView) findViewById(R.id.tv_SN);

        tv_lon_c = (TextView) findViewById(R.id.tv_lon_c);
        tv_alpha_c = (TextView) findViewById(R.id.tv_alpha_c);

        if (_mode == 0) {
            findViewById(R.id.twopointsset_1).setVisibility(View.GONE);
            findViewById(R.id.twopointsset_2).setVisibility(View.GONE);
            findViewById(R.id.twopointsset_3).setVisibility(View.GONE);
            findViewById(R.id.twopointsset_4).setVisibility(View.GONE);
        } else {
            findViewById(R.id.twopointsset_10).setVisibility(View.GONE);
            findViewById(R.id.twopointsset_11).setVisibility(View.GONE);
        }

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @Override
    public void initData() {//必须调用
        refresh();
    }

    void refresh() {

        if (_mode == 0) {
            tv_ctnipt.setText(mPAZ.lat_o + "");
            tv_lon_c.setText(mPAZ.lon_c + "");
            tv_alpha_c.setText(mPAZ.alpha_c + "");
            tv_utmc.setText(mPAZ.utmc + "");
            tv_TY.setText(mPAZ.TY + "");
            tv_EW.setText(mPAZ.EW + "");
            tv_SN.setText(mPAZ.SN + "");
        } else {
            tv_ctnipt.setText(mSelfInfo.ctnipt + "");
            tv_lat_pt1.setText(mSelfInfo.lat_pt1 + "");
            tv_lat_pt2.setText(mSelfInfo.lat_pt2 + "");
            tv_lon_pt1.setText(mSelfInfo.lon_pt1 + "");
            tv_lon_pt2.setText(mSelfInfo.lon_pt2 + "");
            tv_utmc.setText(mSelfInfo.utmc + "");
            tv_TY.setText(mSelfInfo.TY + "");
            tv_EW.setText(mSelfInfo.EW + "");
            tv_SN.setText(mSelfInfo.SN + "");

        }

    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.twopointsset_0).setOnClickListener(this);
        findViewById(R.id.twopointsset_1).setOnClickListener(this);
        findViewById(R.id.twopointsset_2).setOnClickListener(this);
        findViewById(R.id.twopointsset_3).setOnClickListener(this);
        findViewById(R.id.twopointsset_4).setOnClickListener(this);
        findViewById(R.id.twopointsset_5).setOnClickListener(this);
        findViewById(R.id.twopointsset_6).setOnClickListener(this);
        findViewById(R.id.twopointsset_7).setOnClickListener(this);
        findViewById(R.id.twopointsset_8).setOnClickListener(this);

        findViewById(R.id.twopointsset_10).setOnClickListener(this);
        findViewById(R.id.twopointsset_11).setOnClickListener(this);
    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        TwoPointsFileHandle.getInstance().save(getBaseContext());
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu)), REQUEST_TO_TOP_MENU, false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.twopointsset_0:
                editParm(0);
                break;
            case R.id.twopointsset_1:
                editParm(1);
                break;
            case R.id.twopointsset_2:
                editParm(2);
                break;
            case R.id.twopointsset_3:
                editParm(3);
                break;
            case R.id.twopointsset_4:
                editParm(4);
                break;
            case R.id.twopointsset_5:
                editParm(5);
                break;
            case R.id.twopointsset_6:
                editParm(6);
                break;
            case R.id.twopointsset_7:
                editParm(7);
                break;
            case R.id.twopointsset_8:
                editParm(8);
                break;
            case R.id.twopointsset_10:
                editParm(10);
                break;
            case R.id.twopointsset_11:
                editParm(11);
                break;
            default:
                break;
        }
    }

    private void editParm(int pos) {

        String strT = getString(R.string.twopointsset_t0);
        int REQUEST = REQUEST_TO_CTNIPT;
        if (_mode == 1) {
            double value = mSelfInfo.ctnipt;
            switch (pos) {
                default:
                case 0:
                    intent = EditNumberActivity.createIntentDLimt(context
                            , strT + getString(R.string.parmsetting), value, "", -90, 90);
                    break;
                case 1:
                    strT = getString(R.string.twopointsset_t1);
                    value = mSelfInfo.lat_pt1;
                    REQUEST = REQUEST_TO_LATPT1;
                    intent = EditNumberActivity.createIntentDLimt(context
                            , strT + getString(R.string.parmsetting), value, "", -90, 90);
                    break;
                case 2:
                    strT = getString(R.string.twopointsset_t2);
                    value = mSelfInfo.lat_pt2;
                    REQUEST = REQUEST_TO_LATPT2;
                    intent = EditNumberActivity.createIntentDLimt(context
                            , strT + getString(R.string.parmsetting), value, "", -90, 90);
                    break;
                case 3:
                    strT = getString(R.string.twopointsset_t3);
                    value = mSelfInfo.lon_pt1;
                    REQUEST = REQUEST_TO_LONPT1;
                    intent = EditNumberActivity.createIntentDLimt(context
                            , strT + getString(R.string.parmsetting), value, "", -180, 180);
                    break;
                case 4:
                    strT = getString(R.string.twopointsset_t4);
                    value = mSelfInfo.lon_pt2;
                    REQUEST = REQUEST_TO_LONPT2;
                    intent = EditNumberActivity.createIntentDLimt(context
                            , strT + getString(R.string.parmsetting), value, "", -180, 180);
                    break;
                case 5:
                    strT = getString(R.string.twopointsset_t5);
                    value = mSelfInfo.TY;
                    REQUEST = REQUEST_TO_TY;
                    intent = EditNumberActivity.createIntentD(context
                            , strT + getString(R.string.parmsetting), value, "");
                    break;
                case 6:
                    strT = getString(R.string.twopointsset_t6);
                    value = mSelfInfo.utmc;
                    REQUEST = REQUEST_TO_UTMC;
                    intent = EditNumberActivity.createIntentD(context
                            , strT + getString(R.string.parmsetting), value, "");
                    break;
                case 7:
                    strT = getString(R.string.twopointsset_t7);
                    value = mSelfInfo.EW;
                    REQUEST = REQUEST_TO_EW;
                    intent = EditNumberActivity.createIntent(context
                            , strT + getString(R.string.parmsetting), (int) value, "");
                    break;
                case 8:
                    strT = getString(R.string.twopointsset_t8);
                    value = mSelfInfo.SN;
                    REQUEST = REQUEST_TO_SN;
                    intent = EditNumberActivity.createIntent(context
                            , strT + getString(R.string.parmsetting), (int) value, "");
                    break;
            }
        } else {
            double value = mPAZ.lat_o;
            switch (pos) {
                default:
                case 0:
                    intent = EditNumberActivity.createIntentDLimt(context
                            , strT + getString(R.string.parmsetting), value, "", -90, 90);
                    break;
                case 10:
                    strT = getString(R.string.twopointsset_t10);
                    value = mPAZ.lon_c;
                    REQUEST = REQUEST_TO_LON;
                    intent = EditNumberActivity.createIntentDLimt(context
                            , strT + getString(R.string.parmsetting), value, "", -180, 180);
                    break;
                case 11:
                    strT = getString(R.string.twopointsset_t11);
                    value = mPAZ.alpha_c;
                    REQUEST = REQUEST_TO_ALP;
                    intent = EditNumberActivity.createIntentDLimt(context
                            , strT + getString(R.string.parmsetting), value, "", 0, 360);
                    break;
                case 5:
                    strT = getString(R.string.twopointsset_t5);
                    value = mPAZ.TY;
                    REQUEST = REQUEST_TO_TY;
                    intent = EditNumberActivity.createIntentD(context
                            , strT + getString(R.string.parmsetting), value, "");
                    break;
                case 6:
                    strT = getString(R.string.twopointsset_t6);
                    value = mPAZ.utmc;
                    REQUEST = REQUEST_TO_UTMC;
                    intent = EditNumberActivity.createIntentD(context
                            , strT + getString(R.string.parmsetting), value, "");
                    break;
                case 7:
                    strT = getString(R.string.twopointsset_t7);
                    value = mPAZ.EW;
                    REQUEST = REQUEST_TO_EW;
                    intent = EditNumberActivity.createIntent(context
                            , strT + getString(R.string.parmsetting), (int) value, "");
                    break;
                case 8:
                    strT = getString(R.string.twopointsset_t8);
                    value = mPAZ.SN;
                    REQUEST = REQUEST_TO_SN;
                    intent = EditNumberActivity.createIntent(context
                            , strT + getString(R.string.parmsetting), (int) value, "");
                    break;
            }
        }

        toActivity(intent, REQUEST);
    }


    private final int REQUEST_TO_TOP_MENU = 30;
    private final int REQUEST_TO_CTNIPT = 31;
    private final int REQUEST_TO_LATPT1 = 32;
    private final int REQUEST_TO_LATPT2 = 33;
    private final int REQUEST_TO_LONPT1 = 34;
    private final int REQUEST_TO_LONPT2 = 35;
    private final int REQUEST_TO_UTMC = 36;
    private final int REQUEST_TO_TY = 37;
    private final int REQUEST_TO_EW = 38;
    private final int REQUEST_TO_SN = 39;

    private final int REQUEST_TO_LON = 40;
    private final int REQUEST_TO_ALP = 41;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_CTNIPT:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    if (_mode == 1) {
                        mSelfInfo.ctnipt = Double.valueOf(editedValue);
                    } else {
                        mPAZ.lat_o = Double.valueOf(editedValue);
                    }
                    refresh();
                }
                break;
            case REQUEST_TO_LON:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mPAZ.lon_c = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_ALP:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mPAZ.alpha_c = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_LATPT1:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mSelfInfo.lat_pt1 = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_LATPT2:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mSelfInfo.lat_pt2 = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_LONPT1:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mSelfInfo.lon_pt1 = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_LONPT2:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mSelfInfo.lon_pt2 = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_UTMC:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    if (_mode == 1) {
                        mSelfInfo.utmc = Double.valueOf(editedValue);
                    } else {
                        mPAZ.utmc = Double.valueOf(editedValue);
                    }
                    refresh();
                }
                break;
            case REQUEST_TO_TY:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    if (_mode == 1) {
                        mSelfInfo.TY = Double.valueOf(editedValue);
                    } else {
                        mPAZ.TY = Double.valueOf(editedValue);
                    }
                    refresh();
                }
                break;
            case REQUEST_TO_EW:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);

                    if (_mode == 1) {
                        mSelfInfo.EW = Integer.valueOf(editedValue);
                    } else {
                        mPAZ.EW = Integer.valueOf(editedValue);
                    }
                    refresh();
                }
                break;
            case REQUEST_TO_SN:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    if (_mode == 1) {
                        mSelfInfo.SN = Integer.valueOf(editedValue);
                    } else {
                        mPAZ.SN = Integer.valueOf(editedValue);
                    }
                    refresh();
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:

                            if (_mode == 1) {
                                mSelfInfo.reset();
                            } else {
                                mPAZ.reset();
                            }
                            refresh();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
